<script setup>
// table.vue - 子组件保持不变
const props = defineProps({
  tableData: Array,
  labelData: Array
});

const newTableData = ref([]);
const emit = defineEmits(["loadComplete"]);

onMounted(() => {
  let index = 0;
  const interval = setInterval(() => {
    if (index < props.tableData.length) {
      newTableData.value.push(props.tableData[index]);
      index++;
      emit("loadComplete");
    } else {
      clearInterval(interval);
    }
  }, 310); // 调整间隔时间控制加载速度
});
</script>

<template>
  <div class="table-container">
    <table>
      <thead>
        <tr>
          <th v-for="item in props.labelData" :key="item">
            {{ item }}
          </th>
        </tr>
      </thead>
      <tbody>
        <template v-if="newTableData.length > 0">
          <!-- 如果 newTableData 是二维数组 -->
          <tr
            v-for="(row, rowIndex) in newTableData"
            :key="rowIndex"
            class="row-animate"
          >
            <td
              v-for="(cell, cellIndex) in Array.isArray(row)
                ? row
                : Object.values(row)"
              :key="cellIndex"
            >
              {{ cell }}
            </td>
          </tr>
        </template>

        <!-- 如果没有数据 -->
        <tr v-else>
          <td :colspan="props.labelData.length" class="no-data">暂无数据</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<style scoped lang="scss">
.table-container {
  margin: 12px -16px 0;
  padding: 0 16px;
  overflow-x: auto;

  table {
    width: 100%;
    min-width: 600px;
    border-collapse: collapse;

    th,
    td {
      padding: 10px;
      border: 1px solid #ddd;
      min-width: 120px;
      white-space: nowrap;
    }

    th {
      background: #f4f4f4;
      font-weight: bold;
    }

    .row-animate {
      opacity: 0;
      transform: translateY(20px);
      animation: rowEnter 0.6s forwards;

      @for $i from 1 through 50 {
        &:nth-child(#{$i}) {
          animation-delay: 0.01s * $i;
        }
      }
    }
  }
}

@keyframes rowEnter {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
